import functools, asyncio, logging
from telethon.errors import FloodWaitError, AuthKeyError

log = logging.getLogger(__name__)

def auto_retry(max_retry: int = 3):
    print(f"[DEBUG] 进入函数: auto_retry")
    def deco(fn):
        print(f"[DEBUG] 进入函数: auto_retry.deco")
        @functools.wraps(fn)
        async def wrapper(*args, **kwargs):
            print(f"[DEBUG] 进入函数: auto_retry.wrapper")
            for attempt in range(max_retry):
                try:
                    return await fn(*args, **kwargs)
                except FloodWaitError as e:
                    log.warning('FloodWait %ss, retry=%s', e.seconds, attempt)
                    await asyncio.sleep(e.seconds)
                except (AuthKeyError, OSError) as e:
                    log.error('Fatal %s, giving up', e)
                    raise
            raise RuntimeError('Max retry exceeded')
        return wrapper
    return deco